package com.shopping.mall.core.module.ipAddress.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shopping.mall.base.http.httpclient.util.HttpUtils;
import com.shopping.mall.core.module.ipAddress.entity.IpAddress;
import com.shopping.mall.core.module.ipAddress.entity.IpAddressVO;
import com.shopping.mall.core.module.ipAddress.mapper.IpAddressMapper;
import com.shopping.mall.core.module.ipAddress.service.IpAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shopping.mall.base.http.ResponseState;
import com.shopping.mall.base.http.Result;
import com.shopping.mall.base.util.DateUtils;
import com.shopping.mall.base.util.IPUtils;
import com.shopping.mall.base.util.WrapperUtils;
import com.shopping.mall.core.module.saleData.service.SaleDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  IpAddress服务实现类
 * </p>
 *
 * @author zzy
 * @since 2021-08-30
 */
@Validated
@Service
public class IpAddressServiceImpl extends ServiceImpl<IpAddressMapper, IpAddress> implements IpAddressService {

    @Value(value = "${map.key}")
    private String key;

    @Autowired
    private IpAddressMapper ipAddressMapper;

    @Autowired
    private HttpUtils httpUtils;

    @Autowired
    private SaleDataService dataService;

    /**
     * 分页查询
     * @param current ：页码
     * @param size ：每页条数
     * @param ipAddress ：筛选条件
     * @return IPage<IpAddress>
     */
    @Override
    public IPage<IpAddress> getIpAddressPage(Integer current, Integer size, IpAddressVO ipAddress){
        ipAddress.setTime(null);
        QueryWrapper<IpAddress> queryWrapper = WrapperUtils.queryWrapperByQueryVONoTime(ipAddress);
        if(ipAddress.getBefore()!=null){
            queryWrapper.ge("create_at",new Timestamp(ipAddress.getBefore().getTime()));
        }
        if(ipAddress.getAfter()!=null){
            queryWrapper.le("create_at",new Timestamp(ipAddress.getAfter().getTime()));
        }
        queryWrapper.orderByDesc("create_at");
        Page<IpAddress> ipAddressPage = new Page<>(current,size);
        IPage<IpAddress> ipAddressIPage = ipAddressMapper.selectPage(ipAddressPage, queryWrapper);
        return ipAddressIPage;
    }

    /**
     * 查询所有
     * @param ipAddress ：筛选条件
     * @return List<IpAddress>
     */
    @Override
    public List<IpAddress> getAll(IpAddressVO ipAddress){
        Timestamp time=ipAddress.getTime();
        ipAddress.setTime(null);
        QueryWrapper<IpAddress> queryWrapper = WrapperUtils.queryWrapperByQueryVONoTime(ipAddress);
        if(ipAddress.getBefore()!=null){
            queryWrapper.ge("create_at",new Timestamp(ipAddress.getBefore().getTime()));
        }
        if(ipAddress.getAfter()!=null){
            queryWrapper.le("create_at",new Timestamp(ipAddress.getAfter().getTime()));
        }
        queryWrapper.orderByDesc("create_at");
        return ipAddressMapper.selectList(queryWrapper);
    }

    /**
     * 通过id删除
     * @param id：ipAddressId
     * @return Result<Object>
     */
    @Override
    public Result<Object> deleteById(String id){
        IpAddress ipAddress=ipAddressMapper.selectById(id);
        if(ipAddress==null){
            return Result.error(ResponseState.TARGET_NOT_EXIST.getValue(), ResponseState.TARGET_NOT_EXIST.getMessage());
        }
        ipAddressMapper.deleteById(id);
        return Result.OK();
    }

    /**
    * 批量删除
    * @param ids：id列表
    * @return Result<Object>
    */
    @Override
    public Result<Object> batchDelete(List<String> ids){
        UpdateWrapper<IpAddress> updateWrapper=new UpdateWrapper<>();
        updateWrapper.in("id",ids);
        ipAddressMapper.delete(updateWrapper);
        return Result.OK();
    }

    @Override
    public void add(IpAddress ipAddress, HttpServletRequest request) {
        String ip= IPUtils.getIp(request);
        Map<String,Object> result=getIpAddress(ip);
        ipAddress.setPlace(result);
        ipAddress.setIp(ip);
        ipAddressMapper.insert(ipAddress);
        dataService.visitPage(ipAddress);
    }

    public Map<String, Object> getIpAddress(String ip){
        System.out.println(key);
        String url = "https://restapi.amap.com/v5/ip";
        Map<String, Object> param = new HashMap<>();
        param.put("key", key);
        param.put("type", 4);
        param.put("ip", ip);
        Map<String, Object> result = httpUtils.httpGetReturnMap(url, param);
        System.out.println(result);
        return result;
    }

    public static void main(String[] args) {
        FutureTask<Integer> task=new FutureTask<>(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                return 1;
            }
        });
//        Run,nable runnable = new Runnable(){



        Thread t1=new Thread(task);
        t1.setDaemon(true);

        try {
            System.out.println(task.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }

}